package com.vx.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.activitiFlow.service.IRepositoryBiz;
import com.activitiFlow.service.IRuntimeBiz;
import com.activitiFlow.service.ITaskBiz;
import com.vx.service.IProcessInstanceEntryService;
import com.vx.service.IWorkflowService;
/**
 * 操作流程定义pdmn文件的IProcessInstanceServiceEntry接口实现类
 * @author jacob
 *
 */
@Service("processInstanceEntryService")
public class ProcessInstanceEntryServiceImpl implements IProcessInstanceEntryService {
	
	@Autowired
	private ITaskBiz taskBiz;
	
	@Autowired
	private IRepositoryBiz repositoryBiz;
	
	@Autowired
	private IRuntimeBiz runtimeBiz;
	
	public List<String> getLineNames(String taskId){
		String name=null;
		List<String> lineNames=new ArrayList<String>();
		ActivityImpl activityImpl = getRunActivityNode(taskId);
		List<PvmTransition>  pvmList=activityImpl.getOutgoingTransitions();
		for(PvmTransition pvm:pvmList){
			name=(String) pvm.getProperty("name");
			if(StringUtils.isNotBlank(name)){
				lineNames.add(name);
			}else{
				lineNames.add("默认提交");
			}
		}
		
		return lineNames;
	}

	/**
	 * 根据任务节点获取dpmn文件中当前运行到的节点信息
	 * @param taskId
	 * @return
	 */
	public ActivityImpl getRunActivityNode(String taskId) {
		//根据任务id获取任务信息
		Task task=taskBiz.getTaskByTaskId(taskId);
		//1.根据任务里面的processInstanceId获取流程定义信息，并转成与流程定义相关的dnmn文件信息
		ProcessDefinitionEntity  processDefinitionEntity =(ProcessDefinitionEntity) repositoryBiz.queryProcessDefinitionByTaskId(taskId);
		//2.根据任务id获取当前正在执行的实例对象
		ProcessInstance processInstance=runtimeBiz.getProcessInstanceById(task.getProcessInstanceId());
		//3.根据当前执行到的节点名称，从dpmn中获取节点信息
		ActivityImpl  activityImpl=processDefinitionEntity.findActivity(processInstance.getActivityId());
		return activityImpl;
	}

}
